#include "foodtypecontroller.h"

FoodTypeController::FoodTypeController(QObject *parent)
    : QObject{parent}
{
    accessManager = new QNetworkAccessManager(this);
    connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");//构建json头
    //测试post
    //reply = accessManager->post(request, QByteArray("{\"password\": \"123456\", \"phone\": \"15683141454\"}"));
}
//测试通过
void FoodTypeController::getFoodTypeList()
{
    request.setUrl(QUrl("http://81.70.102.35:8082/user/foodType/list"));
    reply = accessManager->get(request);//发送GET请求
}

void FoodTypeController::finishedSlot(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();
        //qDebug() << "许凡楠大帅哥";
        //qDebug() << bytes;
        QJsonParseError jsonError;
        QJsonDocument doucment = QJsonDocument::fromJson(bytes, &jsonError);  // 转化为 JSON 文档
        QJsonObject object = doucment.object();
        if (object.contains("code"))
        {
            QJsonValue value = object.value("code");  // 获取指定 key 对应的 value
            this->m_messageinfo.code=value.toInt();
        }
        if(object.contains("message"))
        {
            QJsonValue value = object.value("message");
            this->m_messageinfo.message=value.toString();
            qDebug()<<this->m_messageinfo.message;
        }
        if(reply->url().toString()=="http://81.70.102.35:8082/user/foodType/list")
        {
            this->foodTypeList.clear();
            if(object.contains("data"))
            {
                QJsonValue value = object.value("data");
                if(value.isArray())
                {
                    QJsonArray array = value.toArray();
                    int nSize = array.size();
                    for(int i=0;i<nSize;i++)
                    {
                        QJsonValue value = array.at(i);
                        QJsonObject qobject = value.toObject();
                        foodTypeInfo f_typeinfo;
                        if(qobject.contains("version"))
                        {
                            QJsonValue value = qobject.value("version");
                            f_typeinfo.version=value.toInt();
                        }
                        if(qobject.contains("valid"))
                        {
                            QJsonValue value = qobject.value("valid");
                            f_typeinfo.valid=value.toInt();
                        }
                        if(qobject.contains("isDeleted"))
                        {
                            QJsonValue value = qobject.value("isDeleted");
                            f_typeinfo.isDeleted=value.toInt();
                        }
                        if(qobject.contains("id"))
                        {
                            QJsonValue value = qobject.value("id");
                            f_typeinfo.id=value.toInt();
                        }
                        if(qobject.contains("gmtCreate"))
                        {
                            QJsonValue value = qobject.value("gmtCreate");
                            f_typeinfo.gmtCreate=value.toString();
                        }
                        if(qobject.contains("gmtModified"))
                        {
                            QJsonValue value = qobject.value("gmtModified");
                            f_typeinfo.gmtModified=value.toString();
                        }
                        if(qobject.contains("name"))
                        {
                            QJsonValue value = qobject.value("name");
                            f_typeinfo.name=value.toString();
                        }
                        if(qobject.contains("pic"))
                        {
                            QJsonValue value = qobject.value("pic");
                            f_typeinfo.pic=value.toString();
                        }
                        this->foodTypeList.push_back(f_typeinfo);
                    }
                }
            }
            emit getFoodTypeListSuccess();
        }
    }
    else
    {
        qDebug() << "finishedSlot errors here";
        qDebug( "found error .... code: %d\n", (int)reply->error());
        qDebug(qPrintable(reply->errorString()));
    }
    reply->deleteLater();
}
